<?php
namespace Home\Controller;
use Think\Controller;

class ContactController extends \Common\Controller\UserController {
	public function index() {
		$page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize = 10;

		$sqlw = 'userid = ' . session('userid');
		
		$Message = M('message');
        $data = $Message -> where($sqlw) -> order('reply_time DESC') ->limit(($page-1)*$pagesize,$pagesize) -> select();
        $this -> assign('messages', $data);

		$sql = "select count(*) as total from tm_message where ".$sqlw;
		$query = $Message->query($sql);
		$count = $query[0]['total'];
		$this->assign('total', $count);
		
		if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

		$pageurl = '/user/hq.html?';
		
		$this->assign('page', $page);

		$this->assign('pageurl',$pageurl);
        $this->assign('pagesize',$pagesize);
		
		$this -> display();
	}
	
	public function create() {
		if(IS_POST) {
    		$post = I('post.');
    		if(M('message') -> add(array('userid' => session('userid'), 'title' => $post['title'], 'content' => $post['content'], 'createtime' => time()))) {
    			$this -> redirect('/user/hq');
    		} else {
    			$this -> error('提交问题失败!');
    		}
    	}
		$this -> display();
	}
	
	public function show() {
		$id = I('get.id');
    	$this -> assign('message', M('message') -> where(' id = ' . $id) -> find());
    	$this -> display();
	}
}
?>